package com.jams.goodox.activity.mine.personal;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.entity.CompanyBean;
import com.jams.goodox.entity.User;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.ProgressDialogUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.TipAlertDialog;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.Utils;
import com.jams.goodox.widget.EditTextWithDel;
import com.jams.goodox.widget.VisageBeastTextView;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;
/**
 * Created by jams on 2017/5/9
 * ***
 * Tel: ***
 * Desc: 关联企业
 */
public class ConnectPersonalCompanyActivity extends BaseActivity {

    @Bind(R.id.input_really_name)
    EditTextWithDel inputReallyName;
    @Bind(R.id.tv_company_name)
    TextView tvCompany;
    @Bind(R.id.connect_company_confirm)
    VisageBeastTextView connectCompanyConfirm;
    CompanyBean companyBean;

    private User user;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_connect_personal_company;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "关联所在企业");
        Utils.setFilter(inputReallyName);//非法字符过滤器
        user = (User) getIntent().getSerializableExtra("user");
        setData();
    }

    private void setData(){
        if(user == null){
            return;
        }
        inputReallyName.setText(StringUtils.newString(user.getUserName()));
        inputReallyName.setSelection(inputReallyName.getText().length());
        tvCompany.setText(StringUtils.newString(user.getEnterpriseName()));
    }
    @OnClick({R.id.rl_choose_company, R.id.connect_company_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.rl_choose_company:
//                closeKeyboard();
//                startActivityForResult(new Intent(this,ChooseCompanyActivity.class),1992);
//                break;
            case R.id.connect_company_confirm:
                if("1".equals(user.getRepresent()) || "2".equals(user.getRepresent())){
                    //已关联企业
                    TipAlertDialog.showTip(ConnectPersonalCompanyActivity.this,"知道了","关联企业信息不可修改，若要修改请联系企业负责人");
                    return;
                }
                connect();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1992){
            if(resultCode == RESULT_OK && data != null){
                companyBean = (CompanyBean) data.getSerializableExtra("companyBean");
                if(companyBean!=null){
                    tvCompany.setText(companyBean.getCompanyName());
                }
            }
        }
    }

    //关联
    private void connect(){
        if(inputReallyName.getText().toString().equals(user.getUserName()) && tvCompany.getText().toString().equals(user.getEnterpriseName())){
            ToastUtil.show("已申请关联企业，不需要重新申请");
            finish();
            return;
        }
        if(TextUtils.isEmpty(inputReallyName.getText().toString())||inputReallyName.getText().toString().length()<2){
            ToastUtil.show("请输入真实姓名");
            return;
        }
        if(companyBean == null || TextUtils.isEmpty(tvCompany.getText().toString())){
            ToastUtil.show("请选择关联所在***企业");
            return;
        }
        ProgressDialogUtils.show(this,"");
        enqueue(createCallService(MineService.class).connectCompany(SePref.getCommunityId(), companyBean.getCompanyId(),
                inputReallyName.getText().toString()), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                ProgressDialogUtils.dismiss();
                ToastUtil.show("保存成功");
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onError(String msg) {
                ProgressDialogUtils.dismiss();
            }
        });
    }
}
